package cn.iocoder.yudao.module.erp.service.receiptconfig;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.erp.controller.admin.receiptconfig.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.receiptconfig.ReceiptConfigDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.receiptconfig.ReceiptConfigMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * 客户回单配置 Service 实现类
 *
 * @author 天使管理员
 */
@Service
@Validated
public class ReceiptConfigServiceImpl implements ReceiptConfigService {

    @Resource
    private ReceiptConfigMapper receiptConfigMapper;

    @Override
    public String createReceiptConfig(ReceiptConfigSaveReqVO createReqVO) {
        // 插入
        ReceiptConfigDO receiptConfig = BeanUtils.toBean(createReqVO, ReceiptConfigDO.class);
        receiptConfigMapper.insert(receiptConfig);
        // 返回
        return receiptConfig.getId();
    }

    @Override
    public void updateReceiptConfig(ReceiptConfigSaveReqVO updateReqVO) {
        if (updateReqVO.getId()==null) {
            createReceiptConfig(updateReqVO);
        }else {
            ReceiptConfigDO updateObj = BeanUtils.toBean(updateReqVO, ReceiptConfigDO.class);
            receiptConfigMapper.updateById(updateObj);
        }
    }

    @Override
    public void deleteReceiptConfig(String id) {
        // 校验存在
        validateReceiptConfigExists(id);
        // 删除
        receiptConfigMapper.deleteById(id);
    }

    private void validateReceiptConfigExists(String id) {
        if (receiptConfigMapper.selectById(id) == null) {
            throw exception(RECEIPT_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public ReceiptConfigDO getReceiptConfig() {
        List<ReceiptConfigDO> receiptConfigDOList = receiptConfigMapper.selectList();
        if (receiptConfigDOList.size()>0) {
            return receiptConfigDOList.get(0);
        }else {
            return new ReceiptConfigDO();
        }
    }

    @Override
    public PageResult<ReceiptConfigDO> getReceiptConfigPage(ReceiptConfigPageReqVO pageReqVO) {
        return receiptConfigMapper.selectPage(pageReqVO);
    }

}